package com.xzmzhp.service.power.impl;

import cn.hutool.core.collection.CollUtil;
import com.xzmzhp.dao.power.MenuMapper;
import com.xzmzhp.pojo.power.Menu;
import com.xzmzhp.service.power.MenuService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.vo.personnel.PersonnelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: 王源(xm)
 * @create: 2021-05-31 23:21
 * @program: ams
 * @description: 菜单信息逻辑接口实现类
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 根据用户权限查询菜单
     * @param personnelVo 用户信息
     * @return 用户对应菜单信息
     */
    @Override
    public Set<String> selectMenuByCompetence(PersonnelVo personnelVo) {
        Set<String> perms = new HashSet<>();
        String competenceName = personnelVo.getCompetenceName();
        // 管理员拥有所有权限
        String competence = "社长";
        String competences = "副社长";
        if (competence.equals(competenceName) || competences.equals(competenceName)) {
            perms.add("*:*:*");
        }
        else {
            List<String> list = menuMapper.selectMenuByCompetence(personnelVo.getCompetenceId());
            if (StringUtils.isNotEmpty(list)){
                perms =new HashSet<>(list);
            }
        }
        return perms;
    }

    /**
     * 返回路由
     * @param competenceId 权限Id
     * @param menuState 菜单状态
     * @return 路由信息
     */
    @Override
    public List<Menu> selectRoute(Integer competenceId, String menuState) {
        if (StringUtils.isNotNull(competenceId) && StringUtils.isNotNull(menuState)){
            List<Menu> list = menuMapper.selectRoute(competenceId,menuState);
            if (CollUtil.isNotEmpty(list)){
                return list;
            }
        }
        return Collections.emptyList();
    }


}
